package com.example.gkcircle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.gkcircle.App;
import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.adapter.SearchBookAdapter;
import com.example.gkcircle.library.LibraryRequest;
import com.example.gkcircle.library.object.Book;
import com.example.gkcircle.utils.CommonUtil;
import com.example.gkcircle.utils.MySVProgressHUD;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.widget.EditTextWithDelete;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jehu on 16/4/8.
 */
public class SearchBookActivity extends BaseActivity implements View.OnClickListener,AbsListView.OnScrollListener{

    private ImageButton mBackBtn;
    private TextView mSearchBtn;
    private EditTextWithDelete mSearchEdit;
    private SearchBookAdapter mAdapter;
    private ListView mListView;
    private View mHeaderView;
    private View mFooterView;
    private SwipeRefreshLayout mSwipe;
    private int currentPage;
    private String keyword;



    @Override
    protected void initView() {
        setContentView(R.layout.library_search_book_activity);
        this.mBackBtn=(ImageButton)this.findViewById(R.id.search_book_back);
        this.mSearchBtn=(TextView)this.findViewById(R.id.search_book_btn);
        this.mSearchEdit=(EditTextWithDelete)this.findViewById(R.id.search_book_edit);
        this.mListView=(ListView)this.findViewById(R.id.search_book_listview);
        this.mSwipe=(SwipeRefreshLayout)this.findViewById(R.id.search_book_swipe);
        mSwipe.setColorSchemeColors(getResources().getColor(R.color.main), getResources().getColor(R.color.love), getResources().getColor(R.color.wish));
    }

    @Override
    protected void initListener() {
        mBackBtn.setOnClickListener(this);
        mSearchBtn.setOnClickListener(this);
        mListView.setOnScrollListener(this);
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipe.setRefreshing(false);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                position--;
                Bundle bundle=new Bundle();
                Intent intent=new Intent(SearchBookActivity.this,DetailBookActivity.class);
                bundle.putInt("position", position);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void setData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_book_back:
                finish();
                break;
            case R.id.search_book_btn:
                keyword=mSearchEdit.getText().toString().trim();
                currentPage=1;
                if(keyword.isEmpty()){
                    return;
                }
                CommonUtil.hideSoftInputView(this);
                MySVProgressHUD.getInstance(this).showWithStatus(getResources().getString(R.string.searching));
                LibraryRequest.getInstance().getSearchList(keyword, currentPage, "any", new LibraryRequest.IGetSearchListListener() {
                    @Override
                    public void onSuccess(List<Book> object) {
                        Message msg=new Message();
                        if(object.size()>0){
                            if(null!=App.mSearchBookList){
                                App.mSearchBookList.clear();
                                App.mSearchBookList=null;
                                mAdapter=null;
                            }
                            App.mSearchBookList=new ArrayList<Book>();
                            for(Book book :object){
                                App.mSearchBookList.add(book);
                            }
                            msg.what=0;
                            handler.sendMessage(msg);
                        }else{
                            msg.what=1;
                            handler.sendMessage(msg);
                        }
                    }

                    @Override
                    public void onFailure(int code) {
                        Message msg=new Message();
                        msg.what=2;
                        handler.sendMessage(msg);
                    }
                });
                break;
            default:
                break;
        }
    }

    private Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    if(mAdapter!=null){
                        mSwipe.setRefreshing(false);
                        mAdapter.notifyDataSetChanged();
                        return;
                    }
                    //add headerView and footerView
                    if(mHeaderView==null || mFooterView==null){
                        mHeaderView= LayoutInflater.from(SearchBookActivity.this).inflate(R.layout.divider_header_footer, null);
                        mFooterView=LayoutInflater.from(SearchBookActivity.this).inflate(R.layout.divider_header_footer, null);
                        mListView.addHeaderView(mHeaderView, null, false);
                        mListView.addFooterView(mFooterView, null, false);
                    }
                    mSwipe.setRefreshing(false);
                    mAdapter=new SearchBookAdapter(SearchBookActivity.this,App.mSearchBookList);
                    mListView.setAdapter(mAdapter);
                    MySVProgressHUD.getInstance(SearchBookActivity.this).dismiss();
                    break;
                case 1:
                    mSwipe.setRefreshing(false);
                    ToastFactory.getToast(SearchBookActivity.this, getResources().getString(R.string.no_more_data)).show();
                    MySVProgressHUD.getInstance(SearchBookActivity.this).dismiss();
                    break;
                case 2:
                    mSwipe.setRefreshing(false);
                    ToastFactory.getToast(SearchBookActivity.this, getResources().getString(R.string.connection_error)).show();
                    MySVProgressHUD.getInstance(SearchBookActivity.this).dismiss();
                    break;
            }
        }
    };


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                if (mListView.getLastVisiblePosition() == (mListView.getCount() - 1)) {
                    currentPage++;
                    LibraryRequest.getInstance().getSearchList(keyword, currentPage, "any", new LibraryRequest.IGetSearchListListener() {
                        @Override
                        public void onSuccess(List<Book> object) {
                            Message msg=new Message();
                            if(object.size()>0){
                                for(Book book :object){
                                    App.mSearchBookList.add(book);
                                }
                                msg.what=0;
                                handler.sendMessage(msg);
                            }else{
                                msg.what=1;
                                handler.sendMessage(msg);
                            }
                        }
                        @Override
                        public void onFailure(int code) {
                            Message msg=new Message();
                            msg.what=2;
                            handler.sendMessage(msg);
                        }
                    });
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
}
